#!/bin/tcsh -ef
####
#############################################################################
#                                                                           #
# Title: Refine Params Unbend II                                            #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 04/01/2006                                             #
# Last Modification: 04/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 65
#
# MANUAL: This script allows a lengthy brute-force test of various parameter settings to search the optimal parameter combination. The script runs the core of the <I>unbend I</I> script, testing parameters according to the defined limits. It will calculate the QVal for each parameter setting, and determine the parameter that results in the highest QVal.
#
# MANUAL: Be aware that a reference that is too small (boxb1 too small) will result in an artificially high QVal due to noise-correlation. Keep and open eye and mind, when trying to refine boxa1. 
#
# MANUAL: You can run this script on one representative image over night, and the use the determined parameters as default values for other images from the same class of images (e.g. same sample, same microscope, same tilt-angle). To set default values, either edit the system-wide default 2dx_image.cfg, or edit the 2dx_image.cfg file one level up from the current image directories.
#
# RESET: refine_maskb01_doit_1 = "y"
# RESET: refine_boxb1_doit_1 = "y"
# RESET: refine_maskb01_boxb1_doit = "n"
# RESET: refine_maskb02_doit_1 = "n"
# RESET: refine_maskb01_doit_2 = "n"
# RESET: refine_boxb1_doit_2 = "n"
# RESET: refine_maskb01_doit_3 = "n"
# RESET: refine_boxb1_doit_3 = "n"
#
# DISPLAY: maskb01
# DISPLAY: maskb02
# DISPLAY: maskb03
# DISPLAY: maskb04
# DISPLAY: maskb05
# DISPLAY: boxb1
# DISPLAY: quadradb
# DISPLAY: quadpredb
# DISPLAY: RESMIN
# DISPLAY: RESMAX
# DISPLAY: radlim
# DISPLAY: IMAXCOR
# DISPLAY: ISTEP
# DISPLAY: ISTEP_h
# DISPLAY: comment
# DISPLAY: RMAG
# DISPLAY: LCOLOR
# DISPLAY: ccunbend_program
# DISPLAY: refine_maskb01_val
# DISPLAY: refine_maskb02_val
# DISPLAY: refine_boxb1_val
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set boxb1 = ""
set boxb2 = ""
set quadradb = ""
set imagename = ""
set defocus = ""
set nonmaskimagename = ""
set imagenumber = ""
set realcell = ""
set lattice = ""
set holeb = ""
set maskb01 = ""
set maskb02 = ""
set maskb03 = ""
set maskb04 = ""
set maskb05 = ""
set imagesidelength = ""
set magnification = ""
set stepdigitizer = ""
set tempkeep = ""
set RESMIN = ""
set RESMAX = ""
set ALAT = ""
set quadpredb = ""
set radlim = ""
set realang = ""
set treatspotscan = ""
set facthresha = ""
set phacon = ""
set CS = ""
set KV = ""
set domask = ""
set TLTAXIS = ""
set TLTANG = ""
set TANGL = ""
set det_tilt = ""
set IMAXCOR = ""
set ISTEP = ""
set ISTEP_h = ""
set RMAG = ""
set LCOLOR = ""
set ccunbend_program = ""
set refine_maskb01_val = ""
set refine_boxb1_val = ""
set refine_maskb02_val = ""
set refine_maskb01_doit_1 = ""
set refine_boxb1_doit_1 = ""
set refine_maskb01_boxb1_doit = ""
set refine_maskb02_doit_1 = ""
set refine_maskb01_doit_2 = ""
set refine_boxb1_doit_2 = ""
set refine_maskb01_doit_3 = ""
set refine_boxb1_doit_3 = ""
#
#$end_vars
#
#=============================================================================
#=============================================================================
#=============================================================================
#
#=============================================================================
# In preparation, once a better refinement script is available...
# That script should implement the rounds for maskb0X, so that when refining
# maskb02, the first round should be made with the optimal maskb01.
# Ideally, the result from the first round with maskb01 should be saved, so 
# that the second round for the refinement of maskb02 does not each time have 
# to repeat the first round with maskb01.
# Something for the ToDo list, but far below...
#=============================================================================
#
# LABEL: Refine maskb02?
# LEGEND: Flag, defining if maska should be refined, using the following values.
# EXAMPLE: refine_maskb02_doit_1 = "y"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Bool "y;n"
# set refine_maskb02_doit_1 = "y"
#
# LABEL: Refine boxb2?
# LEGEND: Flag, defining if boxa1 should be refined, using the following values.
# EXAMPLE: refine_boxb2_doit_1 = "y"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Bool "y;n"
# set refine_boxb2_doit_1 = "y"
#
# LABEL: Refine maskb02?
# LEGEND: Flag, defining if maska should be refined, using the following values.
# EXAMPLE: refine_maskb02_doit_2 = "y"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Bool "y;n"
# set refine_maskb02_doit_2 = "y"
#
# LABEL: Refine boxb2?
# LEGEND: Flag, defining if boxa1 should be refined, using the following values.
# EXAMPLE: refine_boxb2_doit_2 = "y"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Bool "y;n"
# set refine_boxb2_doit_2 = "y"
#
# LABEL: Refine maskb03?
# LEGEND: Flag, defining if maska should be refined, using the following values.
# EXAMPLE: refine_maskb03_doit_3 = "y"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Bool "y;n"
# set refine_maskb03_doit_1 = "y"
#
# LABEL: Refine maskb04?
# LEGEND: Flag, defining if maska should be refined, using the following values.
# EXAMPLE: refine_maskb04_doit_3 = "y"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Bool "y;n"
# set refine_maskb04_doit_1 = "y"
#
# LABEL: Refine maskb05?
# LEGEND: Flag, defining if maska should be refined, using the following values.
# EXAMPLE: refine_maskb05_doit_3 = "y"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Bool "y;n"
# set refine_maskb05_doit_1 = "y"
#
# LABEL: maskb02 refinement values (start, end, step, factor)
# LEGEND:  Fourier filter radius for the first unbending, in RLU.
# EXAMPLE: refine_maskb02_val = "5,40,2,1"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Integer "MIN=0;MAX=1000000;DEFAULT=50"
# TYPE: Four_Float "MIN=0;MAX=200;DEFAULT=5;MIN=1;MAX=400;DEFAULT=30;MIN=1;MAX=300;DEFAULT=2;MIN=1.0;MAX=10.0;DEFAULT=1.0"
# RELATION: %OF realcell 2
# set refine_maskb02_val = "5,40,2,1"
#
# LABEL: maskb03 refinement values (start, end, step, factor)
# LEGEND:  Fourier filter radius for the first unbending, in RLU.
# EXAMPLE: refine_maskb03_val = "5,40,2,1"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Integer "MIN=0;MAX=1000000;DEFAULT=50"
# TYPE: Four_Float "MIN=0;MAX=200;DEFAULT=5;MIN=1;MAX=400;DEFAULT=30;MIN=1;MAX=300;DEFAULT=2;MIN=1.0;MAX=10.0;DEFAULT=1.0"
# RELATION: %OF realcell 2
# set refine_maskb03_val = "5,40,2,1"
#
# LABEL: maskb04 refinement values (start, end, step, factor)
# LEGEND:  Fourier filter radius for the first unbending, in RLU.
# EXAMPLE: refine_maskb04_val = "5,40,2,1"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Integer "MIN=0;MAX=1000000;DEFAULT=50"
# TYPE: Four_Float "MIN=0;MAX=200;DEFAULT=5;MIN=1;MAX=400;DEFAULT=30;MIN=1;MAX=300;DEFAULT=2;MIN=1.0;MAX=10.0;DEFAULT=1.0"
# RELATION: %OF realcell 2
# set refine_maskb04_val = "5,40,2,1"
#
# LABEL: maskb05 refinement values (start, end, step, factor)
# LEGEND:  Fourier filter radius for the first unbending, in RLU.
# EXAMPLE: refine_maskb05_val = "5,40,2,1"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Integer "MIN=0;MAX=1000000;DEFAULT=50"
# TYPE: Four_Float "MIN=0;MAX=200;DEFAULT=5;MIN=1;MAX=400;DEFAULT=30;MIN=1;MAX=300;DEFAULT=2;MIN=1.0;MAX=10.0;DEFAULT=1.0"
# RELATION: %OF realcell 2
# set refine_maskb05_val = "5,40,2,1"
#
# LABEL: boxb2 refinement values (start, end, step)
# LEGEND: Diameter of the reference for the first unbending, in pixel.
# EXAMPLE: refine_boxb2_val = "50,700,50,1"
# HELP: http://2dx.org/documentation/2dx-software/parameters/Refine
# TYPE: Four_Float "MIN=5;MAX=500;DEFAULT=50;MIN=5;MAX=4000;DEFAULT=700;MIN=1;MAX=3000;DEFAULT=50;MIN=1.0;MAX=10.0;DEFAULT=1.0"
# RELATION: %OF realcell 2
# set refine_boxb2_val = "50,400,50,1"
#
#=============================================================================
#=============================================================================
#=============================================================================
#
set createmaskinfo = "n"
#
#=============================================================================
#=============================================================================
#=============================================================================
#
set scriptname = 2dx_refine_unbend2
#
\rm -f LOGS/${scriptname}.results
#
set IS_2DX = yes
source ${proc_2dx}/initialize
#
echo "<<@progress: 1>"
#
set date = `date`
echo date = ${date}
#
if ( $domask == 'y' ) then
  set domask = 'n'
  echo domask corrected to ${domask}
endif
#
\rm -f HOLEref.TMP HOLE.TMP
#
set round = 1
if ( ${maskb02} > '0' ) then
  @ round += 1
endif
if ( ${maskb03} > '0' ) then
  @ round += 1
endif
if ( ${maskb04} > '0' ) then
  @ round += 1
endif
if ( ${maskb05} > '0' ) then
  @ round += 1
endif
#
if ( ${treatspotscan} == 'y' ) then
  set valspotscan = '1'
else
  set valspotscan = '0'
endif
#
set rmax = 11000
echo rmax = ${rmax}
#
set reciangle = `echo ${realang} | awk '{ s = 180.0 - $1 } END { print s } '`
echo reciangle = ${reciangle}
#
set stea    = ${stepdigitizer}
echo stea   = ${stea}
#
set steb   = ${stea}
echo steb = ${steb}
#
set imagecenterx = `echo ${imagesidelength} | awk '{ s = int( $1 / 2 ) } END { print s }'`
set imagecentery = ${imagecenterx}
#
set rtemp1 = ${imagesidelength}
@ rtemp1 /= 2
set rtemp2 = ${rtemp1}
@ rtemp1 -= 13
@ rtemp2 += 12
# this gives a pattern diameter of 25 pixels.
set patlabel = ${rtemp1},${rtemp2},${rtemp1},${rtemp2}
echo patlabel = ${patlabel}
#
set rtemp1 = ${imagesidelength}
@ rtemp1 /= 2
set rtemp2 = ${rtemp1}
@ rtemp1 -= 400
@ rtemp2 += 399
# this gives a pattern diameter of 800 or 400 pixels.
set boxlabel = ${rtemp1},${rtemp2},${rtemp1},${rtemp2}
echo boxlabel = ${boxlabel}
#
if ( ! -d SCRATCH ) then
  ${proc_2dx}/protest "ERROR: SCRATCH does not exist."
endif
#
set quadradbx = `echo ${quadradb} | sed 's/,/ /g' | awk '{ s = int( $1 ) } END { print s }'`
set quadradby = `echo ${quadradb} | sed 's/,/ /g' | awk '{ s = int( $2 ) } END { print s }'`
#
#############################################################################
#                                                                           #
#  UNBEND II expects the existence of FFTIR/cor${imagename}.unbend1.fft.mrc #
#                                                                           #
#############################################################################
#
if ( ! -e FFTIR/cor${imagename}_unbend1_fft.mrc ) then
  ${proc_2dx}/protest "ERROR: FFTIR/cor${imagename}_unbend1_fft.mrc not existing."
endif
#
\cp -f FFTIR/cor${imagename}_unbend1_fft.mrc FFTIR/cor${imagename}_fft.mrc
#
echo "<<@progress: 5>>"
#
#############################################################################
##### BEGINNING OF THE REFINEMENT 
#############################################################################
#
set locround = ${round}
set scriptname = "2dx_refine_unbend2"
#
set dowarn = 'n'
set final_round = "n"
#
if ( ${refine_maskb01_doit_1} == 'y' ) then
  set refine_now = "maskb01"
  source ${proc_2dx}/2dx_refine_unbend2_sub.com
endif
#
if ( ${refine_boxb1_doit_1} == 'y' ) then
  set refine_now = "boxb1"
  source ${proc_2dx}/2dx_refine_unbend2_sub.com
  set dowarn = 'y'
endif
#
if ( ${refine_maskb01_boxb1_doit} == 'y' ) then
  set refine_now = "maskb01_boxb1"
  source ${proc_2dx}/2dx_refine_unbend2_sub.com
  set dowarn = 'y'
endif
#
if ( ${refine_maskb01_doit_2} == 'y' ) then
  set refine_now = "maskb01"
  source ${proc_2dx}/2dx_refine_unbend2_sub.com
endif
#
if ( ${refine_boxb1_doit_2} == 'y' ) then
  set refine_now = "boxb1"
  source ${proc_2dx}/2dx_refine_unbend2_sub.com
  set dowarn = 'y'
endif
#
if ( ${refine_maskb01_doit_3} == 'y' ) then
  set refine_now = "maskb01"
  source ${proc_2dx}/2dx_refine_unbend2_sub.com
endif
#
if ( ${refine_boxb1_doit_3} == 'y' ) then
  set refine_now = "boxb1"
  source ${proc_2dx}/2dx_refine_unbend2_sub.com
  set dowarn = 'y'
endif
#
if ( ${refine_maskb02_doit_1} == 'y' ) then
  set refine_now = "maskb02"
  source ${proc_2dx}/2dx_refine_unbend2_sub.com
endif
#
# if ( ${refine_boxb2_doit_1} == 'y' ) then
#   set refine_now = "boxb2"
#   source ${proc_2dx}/2dx_refine_unbend2_sub.com
# endif
# #
# if ( ${refine_maskb02_doit_2} == 'y' ) then
#   set refine_now = "maskb02"
#   source ${proc_2dx}/2dx_refine_unbend2_sub.com
# endif
# #
# if ( ${refine_boxb2_doit_2} == 'y' ) then
#   set refine_now = "boxb2"
#   source ${proc_2dx}/2dx_refine_unbend2_sub.com
# endif
# #
# if ( ${refine_maskb03_doit_1} == 'y' ) then
#   set refine_now = "maskb03"
#   source ${proc_2dx}/2dx_refine_unbend2_sub.com
# endif
# #
# if ( ${refine_maskb04_doit_1} == 'y' ) then
#   set refine_now = "maskb04"
#   source ${proc_2dx}/2dx_refine_unbend2_sub.com
# endif
# #
# if ( ${refine_maskb05_doit_1} == 'y' ) then
#   set refine_now = "maskb05"
#   source ${proc_2dx}/2dx_refine_unbend2_sub.com
# endif
#
#
if ( ${dowarn} == 'y' ) then
  echo "# WARNING: You are refining the reference size." >> LOGS/${scriptname}.results
  echo "# WARNING: Warning: A too small reference will give an artificially good QVal due to noise correlation." >> LOGS/${scriptname}.results
endif
#
#
#################################################################################
${proc_2dx}/linblock "Again, with best parameter: maskb01=${maskb01}, maskb02=${maskb02}, boxb1=${boxb1}, boxb2=${boxb2}"
#################################################################################
#
set final_round = "y"
#
source ${proc_2dx}/2dx_unbend2_sub.com
#
#
echo "<<@progress: 100>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
